<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 规格ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goods/goodsSpec.class.php');

class ajax_goodsSpec {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品规格
                    $this->searchGoodsSpec();
                    break;
			    case 2: //添加和修改商品规格
                    $this->saveGoodsSpec();
                    break;
                case 4: //删除商品规格
                    $this->deleteGoodsSpec();
                    break;
                case 5: //规格的使用状态
                    $this->statusGoodsSpec();
                    break;
                case 6: //待编辑信息查询
                    $this->searchOneGoodsSpec();
                    break;
                case 7: //最大序列号查询查询
                    $this->seachsort();
                    break;
                case 8:	//检查规格是否存在商品
                	$this->checkSpec();
                	break;    
            }

        }

    }
    /**
     * 查询商品规格
     */
    private function searchGoodsSpec() {

        global $G_PGLOB;
        
		$specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
		
		$_REQUEST['isSpec'] = 1;

		$result_data  = $specHandler->goodsSpecGroupList( $_REQUEST ) ;

		$result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {

			$edit_permission = $G_PGLOB->has_permission(512);
            foreach($result_data as &$s_l) {
            	//error_log($s_l['specVal']);
            	if($edit_permission){
	        		$result_array['rows'][] = array(
	                    'id'   => $s_l['specGroupID'],
	                    'cell' => array(
                			'<a href="javascript: void(0);" onclick="goodsSpec.edit(' . $s_l['specGroupID'] . ')">维护值</a>&nbsp;' ,
                			$s_l['specGroupName'],
                			$s_l['specVal'],
                			$s_l['sortOrder'],
                			$s_l['useFlg'] == 1 ?
                			'<a href="javascript: void(0);" onclick="goodsSpec.doAction(' . $s_l['specGroupID'] . ', 0) " >启用</a>' :
                			'<a href="javascript: void(0);" onclick="goodsSpec.doAction(' . $s_l['specGroupID'] . ', 1 ) " >禁用</a>'
						)
	                    
	                );
            	}else{
            		$result_array['rows'][] = array(
	                    'id'   => $s_l['specGroupID'],
	                    'cell' => array(
	            			$s_l['specGroupID'] ,
	            			$s_l['specGroupName'],
	            			$s_l['specVal'],
	            			$s_l['sortOrder'],
	            			$s_l['useFlg'] == 1 ?
	            			'启用' :
	            			'禁用'
	                    )
	                );
            	}
            }
        }

        echo json_encode( $result_array );
    }
    
    /**
     * 规格保存和更新
     *
     */
    private function saveGoodsSpec () {
    	global $G_PGLOB;

    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
    	$result_array = $specHandler->saveGoodsSpec( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
    
	/**
	 * 删除规格
	 *
	 */
	private function deleteGoodsSpec () {
		global $G_PGLOB;
		
    	$specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
    	$result_array = $specHandler->deleteSpecGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	/**
	 * 修改规格的使用状态
	 *
	 */
	private function statusGoodsSpec () {  
		global $G_PGLOB;
		
    	$specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
    	$result_array = $specHandler->changeUse( $_REQUEST ) ;
    	echo json_encode($result_array);
	}

    /*
     * 待编辑信息chax
     *
     * */
    private  function searchOneGoodsSpec(){

        global $G_PGLOB;

        $arg_array = array('specGroupID' => $_REQUEST['specID']);
        $specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
        $result_data  = $specHandler->goodsSpecGroupList( $arg_array ) ;

        $result = array(
            'specID'	  => $result_data[0]['specGroupID'],
            'specName'	  => $result_data[0]['specGroupName'],
            'sortOrder'	  => $result_data[0]['sortOrder'],
            'useFlg'	  => $result_data[0]['useFlg'],
            'isColor'	  => $result_data[0]['isColor'],
            'specArray'   => $result_data[0]['specArray'],
        );

        echo json_encode($result);
    }

   //最大序列号查询
    private function seachsort(){

        global $G_PGLOB;
        
        $specHandler = new goodsSpec( $G_PGLOB->DBCA ) ;
        
        $sortOrder = $specHandler->maxSortOrder();

        echo json_encode(array('sortOrder' => $sortOrder));
    }
    
    /**
     * 检查规格是否存在商品
     *
     */
    private function checkSpec(){
    	global $G_PGLOB;

    	$check_spec = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_spec_relation WHERE specGroupID = ? AND specID = ?', 'ii', $_REQUEST['specGroupID'], $_REQUEST['specID']);
    	
    	if ($check_spec) {
    		echo json_encode(array('code' => 400, 'message' => '当前规格值存在商品，不能删除'));
    	} else {
    		echo json_encode(array('code' => 200, 'message' => '可以删除'));
    	}
    }
}